package CreationMode.configFactory.coffee;

import CreationMode.simpleFactory.coffee.CoffeeFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class FactoryCoffee {
    private static Map<String,Coffee> coffeeMap = new HashMap<>();

    static {
        Properties properties = new Properties();
        InputStream inputStream = CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");

        try {
            properties.load(inputStream);
            Set<Object> keys = properties.keySet();

            for (Object key: keys){
                 String className = properties.getProperty((String) key);
                 Class clazz = Class.forName(className);
                 Coffee coffee =(Coffee) clazz.getDeclaredConstructor().newInstance();
                 coffeeMap.put((String) key, coffee);
            }


        } catch (Exception err){
            err.printStackTrace();
        }
    }

    public static Coffee createCoffee(String coffeeType){
         return coffeeMap.get(coffeeType);
    }
}
